import 'package:flutter/widgets.dart';

enum PeriodUnit{
  day("天"),
  month("月"),
  year("年");
  final String displayName;
  const PeriodUnit(this.displayName);

  get firstUppercaseChar{
    return name[0].toUpperCase();
  }
}

class SingleUnitPeriod {
  final PeriodUnit unit;
  final int value;
  SingleUnitPeriod(this.unit, this.value);

  factory SingleUnitPeriod.parse(String str){
    var replaceFirst = str.replaceFirst("P", "");
    var lastChar = replaceFirst.characters.last;
    var value = int.parse(replaceFirst.substring(0, replaceFirst.length - 1));
    switch(lastChar){
      case "D":
        return SingleUnitPeriod(PeriodUnit.day, value);
      case "M":
        return SingleUnitPeriod(PeriodUnit.month, value);
      case "Y":
        return SingleUnitPeriod(PeriodUnit.year, value);
      default:
        throw Exception("Invalid period string $str");
    }
  }

  @override
  String toString() {
    return "P$value${unit.firstUppercaseChar}";
  }

  String toDisplayString(){
    return "$value${unit.displayName}";
  }
}